using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace GetKlient.services
{
    public class KLConfigService 
    {
        private readonly Models.PFlibDbContext _dbContext;

        public KLConfigService(Models.PFlibDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public async Task<Models.KLConfig> GetKlconfigInfo()
        {
            try
            {
                return await _dbContext.KLConfig.OrderByDescending(k =>k.ID).FirstOrDefaultAsync();;
            }
            catch (System.Exception ex)
            {
                throw new System.Exception($"查询密钥信息失败: {ex.Message}");
            }
        }

        public async Task UpdateKlConfig(string accessToken,int id)
        {
            try
            {
                var kcConfig = await _dbContext.KLConfig.FindAsync(id);
                if (kcConfig!= null)
                {
                    kcConfig.AccessToken = accessToken;
                    await _dbContext.SaveChangesAsync();
                }
            }
            catch (System.Exception ex)
            {
                throw new System.Exception($"更新密钥失败：{ex.Message}", ex);
            }
        }
    }
}